<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单ajax类
 * @package  ajax/backend/bill
 * @author   allen.qiang
 */

quanli_require('module/base/bill/billSale.class.php');
quanli_require('app_class/goodsHandler.class.php');
quanli_require('module/base/system/sysUser.class.php');
quanli_require('quanliadmin/comm/exportExcel_fun.php');

class ajax_billSale {

    public function execute()
    {
        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg']))
        {
            switch($_REQUEST['operFlg']) {

                case 1: //新增或修改报价单
                    $this->searchList();
                    break;

                case 2: //修改状态
                    $this->chanageStatus();
                    break;

                case 3: //查询商品
                    $this->searchGoods();
                    break;

                case 4: //保存报价单
                    $this->save();
                    break;

                case 5: //导出
                    $this->excelout();
                    break;

                case 6: //修改价格
                    $this->savePrice();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }
        } else {
            returnJsonData('参数错误');
        }
    }

    /**
     * 查询报价单
     */
    private function searchList ()
    {
        global $G_PGLOB;

        $billSale = new billSale( $G_PGLOB->DBCA );
        if($_REQUEST['type'] == 1){
            $_REQUEST['saleStatus'] = '1,2,3,4';
        }
        $res_arr = $billSale->searchList( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($res_arr) {

            foreach($res_arr as &$sale)
            {
                $statusName = '';
                switch ($sale->saleStatus){
                    case 0: $statusName = '创建'; break;
                    case 1: $statusName = '已审核'; break;
                    case 2: $statusName = '已生产'; break;
                    case 3: $statusName = '已发货'; break;
                    case 4: $statusName = '已收货'; break;
                    case 8: $statusName = '关闭'; break;
                    case 9: $statusName = '取消'; break;
                }

                $operHtml = '<a href="javascript:void(0);" onclick="sale.detail(\''.$sale->saleCode.'\')">详情</a>&nbsp;&nbsp;';
                if($sale->saleStatus == 0){
                    $operHtml .= '<a href="javascript:void(0);" onclick="sale.audit(\''.$sale->saleCode.'\')">审核</a>&nbsp;&nbsp;';
                    $operHtml .= '<a href="javascript:void(0);" onclick="sale.cancel(\''.$sale->saleCode.'\')">取消</a>&nbsp;';
                }
                if($sale->saleStatus == 1){
                    $operHtml .= '<a href="javascript:void(0);" onclick="sale.product(\''.$sale->saleCode.'\')">生产</a>&nbsp;&nbsp;';
                }
                if($_REQUEST['type'] == 1){
                    $result_array['rows'][] = array(
                        'id' => $sale->saleCode,
                        'cell' => array(
                            $operHtml,
                            $statusName,
                            $sale->saleCode,
                            $sale->saleName,
                            $sale->connect_name,
                            $sale->area,
                            $sale->categoryName,
                            $sale->createTime
                        )
                    );
                }else{
                    $result_array['rows'][] = array(
                        'id' => $sale->saleCode,
                        'cell' => array(
                            $operHtml,
                            $statusName,
                            $sale->saleCode,
                            $sale->saleName,
                            $sale->company_name,
                            $sale->connect_name,
                            $sale->area,
                            $sale->categoryName,
                            $sale->createTime
                        )
                    );
                }
            }
        }
        echo json_encode($result_array);
    }

    //修改状态
    public function chanageStatus(){
        global $G_PGLOB;
        $billSale = new billSale( $G_PGLOB->DBCA );
        $param = $_REQUEST;
        $param['saleCode'] = $param['code'];
        $param['status'] = $param['status'];
        $param['operator'] = $G_PGLOB->currentUserID;

        $res = $billSale->changeStatus($param);
        if($res && $res['code'] == 200){

            if($_REQUEST['status'] == 9){
                $msg = '取消成功';
            }elseif($_REQUEST['status'] == 5){
                $msg = '收货成功';
            }else{
                $msg = ''; $remark = '';
                if($_REQUEST['status'] == 1){
                    $msg = '审核成功';
                    $remark = '您的销售订单已经审核通过，点击查看详情';
                }
                if($_REQUEST['status'] == 2){
                    $msg = '生产成功';
                    $remark = '您的销售订单已经开始生产，点击查看详情';
                }
                if($_REQUEST['status'] == 3){
                    $msg = '发货成功';
                    $remark = '您的销售订单已经发货，点击查看详情';
                }

                try{
                    quanli_include('module/base/company/companyMaster.class.php');
                    $company = new companyMaster();
                    $arg = array('company_id' => $res['data']->companyId);
                    $compay_result = $company->search( $arg );
                    if($compay_result && isset($compay_result[0]) && !empty($compay_result[0]['wx_openid'])){
                        quanli_include('framework/wx/wxHelp.php');
                        $params = array(
                            'openId' => $compay_result[0]['wx_openid'],
                            'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=3&code='.$_REQUEST['code'],
                            'title' => '销售订单提醒',
                            'code' => $_REQUEST['code'],
                            'date' => date('Y-m-d'),
                            'remark' => $remark
                        );
                        wxHelp::wxMessage($params);

                        /*推送给对应客户的销售员微信账号*/
                        $user_master = new sysUser($G_PGLOB->DBCA);//实例用户类
                        $param = array('userID' => $compay_result[0]['userID'], 'useFlg' => 1);
                        $temp_user = $user_master->searchUserInfo($param);
                        if ($temp_user[0]['isSalesman'] == '1' && $temp_user[0]['wx_openid'] != ''){
                            $params = array(
                                'openId' => $temp_user[0]['wx_openid'],
                                'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=3&code='.$_REQUEST['code'],
                                'title' => '销售订单提醒',
                                'code' => $_REQUEST['code'],
                                'date' => date('Y-m-d'),
                                'remark' => $remark
                            );
                            wxHelp::wxMessage($params);
                        }
                    }
                }catch (Exception $e){ elog($e->getMessage()); }
            }

            returnJsonData($msg, 200);
        }
        returnJsonData('操作失败', 400);
    }

    //保存报价单
    public function save(){

        if(!isset($_REQUEST['companyId'],$_REQUEST['companyName'],$_REQUEST['saleName'],$_REQUEST['area'],
                $_REQUEST['request'],$_REQUEST['requestDate'],$_REQUEST['receiver'],$_REQUEST['receivMobile'],
                $_REQUEST['province'],$_REQUEST['city'],$_REQUEST['county'],$_REQUEST['address'],
                $_REQUEST['categoryName'],$_REQUEST['expressId'],
                $_REQUEST['dtl']) || !is_array($_REQUEST['dtl']))
        {
            returnJsonData('缺少参数', 400);
        }

        global $G_PGLOB;

        $billSale = new billSale( $G_PGLOB->DBCA );
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $res_arr = $billSale->save( $_REQUEST );
        if($res_arr['code'] == 200){
            returnJsonData('保存成功', 200);
        }
        returnJsonData('保存失败', 400);
    }

    public function excelout(){
        global $G_PGLOB;

        $billSale = new billSale( $G_PGLOB->DBCA );

        $data = $billSale->searchListExcel( $_REQUEST );
        $xlsName="销售订单列表";//文件名
        $xlsCell=array(
            array('saleCode','销售单号'),
            array('createTime','创建日期'),
            array('userName','销售员姓名'),
            array('saleStatus','销售单状态'),
            array('company_name','客户抬头'),
            array('total_amount','销售订单金额'),
            array('freightCount','代收运费'),
            array('saleName','项目名称'),
            array('area','施工面积'),
            array('categoryName','涂装体系'),
            array('requestDate','要求完成时间'),
            array('request','制作内容及要求'),
            array('expressId','物流方式'),
            array('receiver','收货人'),
            array('receivMobile','收货人电话'),
            array('provinceName','收货省份'),
            array('cityName','收货城市'),
            array('countyName','收货区'),
            array('address','详细地址'),
            array('remark','备注'),

            array('productSeries','产品系列'),
            array('goodsName','材料型号品名'),
            array('color','颜色'),
            array('colorDis','对色标准'),
            array('packageName','包装'),
            array('specName','规格'),
            array('price','产品单价'),
            array('beatingCost','打浆费用'),
            array('actualPrice','实际单价'),
            array('amount','订货数量'),
            array('sendAmount','发货数量'),
        );
        exportExcel($xlsName,$xlsCell,$data,2);

    }

    //修改商品价格
    public function savePrice(){
        global $G_PGLOB;
        $editFlg = $G_PGLOB->has_permission('60012');
        if(!$editFlg){
            returnJsonData('您没有权限', 400);
        }
        $billSale = new billSale( $G_PGLOB->DBCA );
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $res_arr = $billSale->savePrice($_REQUEST);
        if($res_arr['code'] == 200){
            returnJsonData('修改成功', 200);
        }
        returnJsonData('修改失败', 400);
    }
}
